package com.wgts.wgts.mvp.view.activity.message;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerMessageActivityComponent;
import com.wgts.wgts.dagger.module.MessageActivityModule;
import com.wgts.wgts.mvp.presenter.IMessageActivityPresenter;
import com.wgts.wgts.mvp.view.IMessageActivityView;
import com.wgts.wgts.mvp.view.activity.webactivity.WebActivity;
import com.wgts.wgts.netmodle.resp.MessageListEntity;
import com.wgts.wgts.netmodle.resp.UnreadMessageEntity;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

/*
 *
 *
 *   data:2018/10/920:29
 *
 */public class MessageActivity extends BaseActivity implements IMessageActivityView{
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.inform_image)
    RelativeLayout informImage;
    @BindView(R.id.inform_ly)
    RelativeLayout informLy;
    @BindView(R.id.property_image)
    RelativeLayout propertyImage;
    @BindView(R.id.property_ly)
    RelativeLayout propertyLy;
    @BindView(R.id.help_center_image)
    RelativeLayout helpCenterImage;
    @BindView(R.id.help_center_ly)
    RelativeLayout helpCenterLy;
    @BindView(R.id.event_image)
    RelativeLayout eventImage;
    @BindView(R.id.event_ly)
    RelativeLayout eventLy;
    private QBadgeView informBadgeView, propertyBadgeView, eventBadgeView;

    @Inject
    IMessageActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_activity);
        ButterKnife.bind(this);
        includeTitle.setText("消息");
        includeText.setText("全部已读");
        includeText.setTextColor(ContextCompat.getColor(this, R.color.bulue_copy));
        initView();

    }

    @Override
    public void init() {
        DaggerMessageActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .messageActivityModule(new MessageActivityModule(this))
                .build()
                .inject(this);
    }

    private void initView() {
        initQBadgeView();
        presenter.unreadMessage("NOTICE,ACTIVITY,CAPITAL");
    }

    @OnClick({R.id.back_btn_ly, R.id.include_right_ly, R.id.inform_ly, R.id.property_ly,
            R.id.help_center_ly, R.id.event_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.include_right_ly:
                presenter.clearMessage("NOTICE,ACTIVITY,CAPITAL");
                break;
            case R.id.inform_ly:
                presenter.clearMessage("NOTICE");
                Intent intent = new Intent(MessageActivity.this, MessageDetailsActivity.class);
                intent.putExtra(MessageDetailsActivity.CODE_TYPE, "NOTICE");
                startActivity(intent);
                break;
            case R.id.property_ly:
                presenter.clearMessage("CAPITAL");
                Intent intent1 = new Intent(MessageActivity.this, MessageDetailsActivity.class);
                intent1.putExtra(MessageDetailsActivity.CODE_TYPE, "CAPITAL");
                startActivity(intent1);
                break;
            case R.id.help_center_ly:
                Intent helpIntent = new Intent(MessageActivity.this, WebActivity.class);
                helpIntent.putExtra(WebActivity.TITLE, "帮助中心");
                helpIntent.putExtra(WebActivity.ACTION_URL, "https://wap.mohuyoupin.com/help_center.html");
                startActivity(helpIntent);
                break;
            case R.id.event_ly:
                presenter.clearMessage("ACTIVITY");
                Intent intent2 = new Intent(MessageActivity.this, MessageDetailsActivity.class);
                intent2.putExtra(MessageDetailsActivity.CODE_TYPE, "ACTIVITY");
                startActivity(intent2);
                break;
        }
    }

    private void initQBadgeView() {
        informBadgeView = new QBadgeView(this);
        informBadgeView.bindTarget(informImage);
        informBadgeView.setBadgeTextColor(Color.parseColor("#FFFFFF"));
        informBadgeView.setBadgeNumber(0);
        informBadgeView.setExactMode(false);//设置是否显示精确模式数值
        informBadgeView.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
            @Override
            public void onDragStateChanged(int dragState, Badge badge, View targetView) {
            }
        });
        propertyBadgeView = new QBadgeView(this);
        propertyBadgeView.bindTarget(propertyImage);
        propertyBadgeView.setBadgeTextColor(Color.parseColor("#FFFFFF"));
        propertyBadgeView.setBadgeNumber(0);
        propertyBadgeView.setExactMode(false);//设置是否显示精确模式数值
        propertyBadgeView.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
            @Override
            public void onDragStateChanged(int dragState, Badge badge, View targetView) {
            }
        });
        eventBadgeView = new QBadgeView(this);
        eventBadgeView.bindTarget(eventImage);
        eventBadgeView.setBadgeTextColor(Color.parseColor("#FFFFFF"));
        eventBadgeView.setBadgeNumber(0);
        eventBadgeView.setExactMode(false);//设置是否显示精确模式数值
        eventBadgeView.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
            @Override
            public void onDragStateChanged(int dragState, Badge badge, View targetView) {

            }
        });
    }

    @Override
    public void onError(int status, String msg) {
        MoHuToast.showToast(msg);
    }

    @Override
    public void unreadMessage(List<UnreadMessageEntity> entity) {
        for (int i = 0; i < entity.size(); i++){
            switch (entity.get(i).typeCode){
                case "NOTICE":
                    informBadgeView.setBadgeNumber(entity.get(i).unreadCount);
                    break;
                case "CAPITAL":
                    propertyBadgeView.setBadgeNumber(entity.get(i).unreadCount);
                    break;
                case "ACTIVITY":
                    eventBadgeView.setBadgeNumber(entity.get(i).unreadCount);
                    break;
            }
        }
    }

    @Override
    public void cleadMessage(String typeCodes) {
        switch (typeCodes){
            case "NOTICE":
                informBadgeView.setBadgeNumber(0);
                break;
            case "CAPITAL":
                propertyBadgeView.setBadgeNumber(0);
                break;
            case "ACTIVITY":
                eventBadgeView.setBadgeNumber(0);
                break;
            case "NOTICE,ACTIVITY,CAPITAL":
                informBadgeView.setBadgeNumber(0);
                propertyBadgeView.setBadgeNumber(0);
                eventBadgeView.setBadgeNumber(0);
                MoHuToast.showToast("清除成功");
                break;
        }
    }

    @Override
    public void initMessageList(List<MessageListEntity> entities, boolean noMore) {

    }

    @Override
    public void loadMore(List<MessageListEntity> entities, boolean noMore) {

    }
}
